package com.maya.medicalwastes.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.maya.medicalwastes.R;
import com.maya.medicalwastes.util.DeviceUtil;
import com.maya.medicalwastes.util.FormatUtils;


/**
 * *
 */
public class CommonMsgDialog extends Dialog implements View.OnClickListener {

    private Context context;
    private View view;
    private int heightRatio = 11;
    private int widthRatio = 7;
    private TextView tvTitle;
    private Button btnClose;
    private LinearLayout llClose;
    private TextView tvMsg;
    private Button btnCancel;
    private Button btnOk;

    private boolean isCancelable = false;

    public CommonMsgDialog(@NonNull Context context) {
        super(context, R.style.AlertDialog);
        this.context = context;
        initView();
        initDate();
    }

    private void initDate() {

    }

    private void initView() {
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        view = layoutInflater.inflate(R.layout.dialog_msg, null);
        setContentView(view);

        tvTitle = findViewById(R.id.tv_title);
        btnClose = findViewById(R.id.btn_close);
        llClose = findViewById(R.id.ll_close);
        tvMsg = findViewById(R.id.tv_msg);
        btnCancel = findViewById(R.id.btn_cancel);
        btnOk = findViewById(R.id.btn_ok);

        llClose.setOnClickListener(this);//关闭按钮
        tvTitle.setText(FormatUtils.getObject("提示"));

        btnOk.setOnClickListener(this);
        btnCancel.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.ll_close) {
            dismiss();
        }

        if (v.getId() == R.id.btn_ok) {
            if (onBtnClickListener != null) {
                onBtnClickListener.onOkClick();
            }
        }

        if (v.getId() == R.id.btn_cancel) {
            if (onBtnClickListener != null) {
                onBtnClickListener.onCancelClick();
            }
            dismiss();
        }
    }

    public void setMsgText(String msg) {
        tvMsg.setText(FormatUtils.getObject(msg));
    }

    public void setTitle(String title) {
        tvTitle.setText(FormatUtils.getObject(title));
    }


    public void popDialog() {
        setCancelable(isCancelable);
//        backgroundAlpha(0.6f);
        show();
        Window window = getWindow();
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        WindowManager.LayoutParams p = window.getAttributes(); // 获取对话框当前的参数值
        p.gravity = Gravity.CENTER;
        p.height = (int) (DeviceUtil.getDisplayMetrics(context).heightPixels * 6 / heightRatio);
        p.width = DeviceUtil.getDisplayMetrics(context).widthPixels * 6 / widthRatio;
        window.setAttributes(p);
    }

    public OnBtnClickListener onBtnClickListener;

    public OnBtnClickListener getOnBtnClickListener() {
        return onBtnClickListener;
    }

    public void setOnBtnClickListener(OnBtnClickListener onBtnClickListener) {
        this.onBtnClickListener = onBtnClickListener;
    }

    public interface OnBtnClickListener {
        void onOkClick();

        void onCancelClick();
    }

    public void setHeightRatio(int heightRatio) {
        this.heightRatio = heightRatio;
    }

    public void setWidthRatio(int widthRatio) {
        this.widthRatio = widthRatio;
    }

    public void setSizeRatio(int widthRatio, int heightRatio) {
        this.widthRatio = widthRatio;
        this.heightRatio = heightRatio;
    }

    @Override
    public void setCancelable(boolean cancelable) {
        isCancelable = cancelable;
    }
}
